var videoFile,imgFile,ue;
var hides=0;
$(function () {
    ue =new UE.ui.Editor({
        initialFrameHeight:300,initialFrameWidth:700
    });
    ue.render("editor");
    $("#uploadVideo").fileinput({

        language: 'zh', //设置语言

        allowedFileExtensions: ['mp3', 'mp4'],//接收的文件后缀

        uploadAsync: true, //默认异步上传

        showUpload:false, //是否显示上传按钮

        showRemove :true, //显示移除按钮

        showPreview :true, //是否显示预览

        showCaption:false,//是否显示标题

        browseClass:"btn btn-primary", //按钮样式

        dropZoneEnabled: false,//是否显示拖拽区域

        maxFileCount:10, //表示允许同时上传的最大文件个数

        enctype:'multipart/form-data',

        validateInitialCount:true,

        previewFileIcon: "<iclass='glyphicon glyphicon-king'></i>",

        msgFilesTooMany: "选择上传的文件数量({n}) 超过允许的最大数值{m}！",

    })
    getUserList();
});
/*重置*/
function resetClick(){
    $(".dis-inebl input").val("");
    $("#userState option:selected").text("全部");
    $("#trySee option:selected").text("全部");
    getUserList();
}
/*查询*/
function getUserList(init){
    JY.Model.loading();
	$("#ticktTable tbody").html("");
    var pageNum=$("#baseForm .pageNum").val();
    var pageSize=$("#baseForm .pageSize").val();
    var userState=$("#userState option:selected").val();
    var trySee=$("#trySee option:selected").val();
    var name=$("#courName").val().trim();
    if($("#userState option:selected").text()=="全部"){
        userState="";
    }
    if($("#trySee option:selected").text()=="全部"){
        trySee="";
    }
	$.ajax({
		url : jypath + "/course/queryCourse",
		type : "post",
		data:{
            "pageNum":pageNum,
            "pageSize":pageSize,
            "courseName":name,
            "state":userState,
            "trySee":trySee
		},
		jsonType : "json",
		success : function(data) {
		    console.log(data)
            console.log("获取课程返回信息")
            var data=data instanceof Object ? data : JSON.parse(data);
            var html="";
            var list=data.obj;
            var results=list.results;
            var pageNum=list.pageNum,
                pageSize=list.pageSize,
                totalRecord=list.totalRecord;
           if(results!=null&&results.length>0){
                var leng=(pageNum-1)*pageSize;//计算序号
                for(var i = 0;i<results.length;i++){
                    var l=results[i];
                    html+="<tr class='tr_color'>";
                    html+="<td class='center ver-m'><label><input type='checkbox' name='ids' value='"+l.courseId+"' class='ace' /> <span class='lbl'></span></label></td>";
                    html+="<td class='center'>"+l.courseName+"</td>";
                    html+="<td class='center'>"+l.courseTages+"</td>";
                    if(l.trySee==1){
                        html+="<td class='center'>可试看</td>";
                    }else if(l.trySee==0){
                        html+="<td class='center'>不可试看</td>";
                    }
                    if(l.state==1){
                        html+="<td class='center'>有效</td>";
                    }else if(l.state==0){
                        html+="<td class='center'>失效</td>";
                    }
                    html+="</tr>";
                }
                $("#ticktTable tbody").append(html);
                JY.Page.setPage("baseForm","pageing",pageSize,pageNum,totalRecord,"getUserList");
                $("#ticktTable .tr_color").click(function(){
                    $(this).addClass("bgf5").siblings(".tr_color").removeClass("bgf5");
                    if($(this).find(".id_color").hasClass("bg6FAED9")){
                        $(this).find(".id_color").removeClass("bg6FAED9")
                    }else{
                        $(this).find(".id_color").addClass("bg6FAED9").parents(".tr_color").siblings(".tr_color").find(".id_color").removeClass("bg6FAED9");
                    }
                });
            }else{
                html+="<tr><td colspan='7' class='center'>没有相关数据</td></tr>";
                $("#ticktTable tbody").append(html);
                $("#baseForm ul").empty();//清空分页
            }
            JY.Model.loadingClose();
		}
	})
}
function setBlogrollImageName(obj){
    videoFile = obj.files[0];
}
function preview(obj) {
    var url= window.URL.createObjectURL(obj.files[0]);
    $(obj).prev().attr("src",url);
    imgFile = obj.files[0];
}
//新增
function addClick(){
    clearData();
    $(".alearyVideo").addClass("hide");
    JY.Model.edit("auForm_edit","新增课程",function(){
        var that=$(this);
        if(JY.Validate.form("generateForm")){
            if($(".preview_ing").attr("src")==""+jypath+"/static/images/back/add_img.jpg"){
                JY.Model.info("请先新增封面图片!");
            }else if($(".file-input").hasClass("file-input-new")){
                JY.Model.info("请先新增视频或音频!");
            }else{
                $(".masks").removeClass("hide");
                $.ajax({
                    url : jypath + "/course/insertCourse",
                    type : "post",
                    data:{
                        "courseName":$("#generateForm input[name='courseName']").val(),
                        "courseTages":$("#generateForm input[name='courseTages']").val(),
                        "trySee":$("#groom").find("option:selected").val(),
                        "state":$("#groomsSel").find("option:selected").val(),
                        "courseNotes":UE.getEditor('editor').getContent(),
                    },
                    success : function(data) {
                        console.log(data)
                        console.log("新增")
                        if(data.res==1){
                            var relevanceId=data.obj;
                            var formData=new FormData();
                            formData.append("imageFile",imgFile);
                            formData.append("videoFile",videoFile);
                            formData.append("relevanceId",relevanceId);
                            formData.append("relevanceTpyeId",5);
                            formData.append("relevanceTpyeDetail",0);
                            formData.append("videoRelevanceTpyeDetail",3);
                            $.ajax({
                                url: jypath + "/uploadVideoAndImage",
                                type: "post",
                                processData: false,
                                contentType: false,
                                data: formData,
                                success: function (data) {
                                    that.dialog("close");
                                    $(".masks").addClass("hide");
                                    JY.Model.info("操作成功!",function(){getUserList();});
                                }
                            })
                        }else{
                            that.dialog("close");
                        }
                    }
                })
            }

        }
    });
}
//编辑
function editClick(){
    clearData();
    var chks =[];
    var doctorId="";
    $('#ticktTable input[name="ids"]:checked').each(function(){
        chks.push($(this).val());
    });
    if(chks.length==0) {
        JY.Model.info("您没有选择任何内容!");
    }else if(chks.length==1){
        var courseId=chks[0];
        $.ajax({
            url : jypath + "/course/queryCourse",
            type : "post",
            data:{
                "courseId":courseId
            },
            jsonType : "json",
            success : function(data) {
                console.log(data)
                var k=data.obj.results[0];
                $("#generateForm input[name='courseName']").val(k.courseName);
                $("#generateForm input[name='courseTages']").val(k.courseTages);
                $("#groom").find("option[value = '"+k.trySee+"']").attr("selected","selected");
                $("#groomsSel").find("option[value = '"+k.state+"']").attr("selected","selected");
                if(k.courseAttachment!=null&&k.courseAttachment!=undefined){
                    var data=k.courseAttachment;
                    var arrImg=data.split(",");
                     $(".alearyVideo").removeClass("hide");
                     $(".alearyVideo video").attr("src",arrImg[0]);
                     hides=1;
                    $(".preview_ing").attr("src",arrImg[1])
                }
                if(k.courseNotes!=null&&k.courseNotes!=undefined){
                    ue.setContent(k.courseNotes);
                }
            }
        })
        JY.Model.edit("auForm_edit","修改课程",function(){
            var that=$(this);
            JY.Model.loading();
            if(imgFile.name == undefined&&videoFile.name==undefined){
                $.ajax({
                    url : jypath + "/course/updateCourse",
                    type : "post",
                    data:{
                        "courseId":courseId,
                        "courseName":$("#generateForm input[name='courseName']").val(),
                        "courseTages":$("#generateForm input[name='courseTages']").val(),
                        "trySee":$("#groom").find("option:selected").val(),
                        "state":$("#groomsSel").find("option:selected").val(),
                        "courseNotes":UE.getEditor('editor').getContent(),
                        "imgPath":"",
                        "path":"",
                    },
                    success : function(data) {
                        if(data.res==1){
                            JY.Model.loadingClose();
                            that.dialog("close");
                            JY.Model.info("操作成功!",function(){getUserList();});
                        }
                    }
                })
            }else{
                $.ajax({
                    url : jypath + "/course/updateCourse",
                    type : "post",
                    data:{
                        "courseId":courseId,
                        "courseName":$("#generateForm input[name='courseName']").val(),
                        "courseTages":$("#generateForm input[name='courseTages']").val(),
                        "trySee":$("#groom").find("option:selected").val(),
                        "state":$("#groomsSel").find("option:selected").val(),
                        "courseNotes":UE.getEditor('editor').getContent(),
                    },
                    success : function(data) {
                        if(data.res==1){
                            var formData=new FormData();
                            if(imgFile.name!=undefined){
                                formData.append("imageFile",imgFile);
                                formData.append("relevanceTpyeDetail",0);
                            }
                            if(videoFile.name!=undefined){
                                formData.append("videoFile",videoFile);
                                formData.append("videoRelevanceTpyeDetail",3);
                            }
                            formData.append("relevanceId",courseId);
                            formData.append("relevanceTpyeId",5);
                            $.ajax({
                                url: jypath + "/uploadVideoAndImage",
                                type: "post",
                                processData: false,
                                contentType: false,
                                data: formData,
                                success: function (data) {
                                    JY.Model.loadingClose();
                                    that.dialog("close");
                                    JY.Model.info("操作成功!",function(){getUserList();});
                                }
                            })
                        }else{
                            JY.Model.loadingClose();
                            that.dialog("close");
                        }
                    }
                })
            }
        });
    }else if(chks.length>1){
        JY.Model.info("一次只能编辑一条信息!");
    }
}
// 启用禁用
function enAbleClick(type){
    var chks =[];
    $('#ticktTable input[name="ids"]:checked').each(function(){
        chks.push($(this).val());
    });
    if(chks.length==0) {
        JY.Model.info("您没有选择任何内容!");
    }else if(chks.length>0){
        var states="";
        if(type==1){
            //启用
            states=1;
        }else if(type==0){
            //禁用
            states=0;
        }
        JY.Model.message("auForm_edits","修改状态",function(){
            var that = $(this);
            $.ajax({
                url : jypath + "/course/updateCourseState",
                type : "post",
                data:{
                    "state":states,
                    "parameter":chks,
                },
                jsonType : "json",
                success : function(data) {
                    console.log(data);
                    if(data.res==1){
                        that.dialog("close");
                        JY.Model.info("操作成功!",function(){getUserList();});
                    }
                }
            })
        });
    }
}
function clearData(){
    $("#generateForm input[name='courseName']").val("");
    $("#generateForm input[name='courseTages']").val("");
    $("#groomsSel").find("option[value = '1']").prop("selected","selected");
    $("#generateForm .preview_ing").attr("src",""+jypath+"/static/images/back/add_img.jpg");
    $("#input1").val("");
    $("#uploadVideo").val("");
    $(".file-input").addClass("file-input-new")
    UE.getEditor('editor').setContent('', false);
    videoFile={};
    imgFile={};
}
